package com.xms.schema.service.optionset;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.xms.core.exception.XmsException;
import com.xms.datacore.service.UpdaterService;
import com.xms.schema.entity.OptionSet;
import com.xms.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * OptionSetUpdaterService
 * </p>
 *
 * @author migo
 * @since 2021/9/8
 */
@CacheConfig(cacheNames = "schema.optionset", keyGenerator = "CacheKeyGenerator")
@Service
public class OptionSetUpdaterService extends UpdaterService<OptionSet> implements IOptionSetUpdaterService {
    private IOptionSetDetailCreaterService _optionSetDetailCreaterService;
    private IOptionSetDetailDeleterService _optionSetDetailDeleterService;

    @Autowired
    public OptionSetUpdaterService(IOptionSetDetailCreaterService optionSetDetailCreaterService,
                                   IOptionSetDetailDeleterService optionSetDetailDeleterService) {
        _optionSetDetailCreaterService = optionSetDetailCreaterService;
        _optionSetDetailDeleterService = optionSetDetailDeleterService;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean update(OptionSet entity) throws XmsException, SQLException {
        boolean success = super.update(entity);
        if (!success || CollectionUtil.notEmpty(entity.getItems())) {
            _optionSetDetailDeleterService.deleteByOptionsetid(entity.getOptionsetid());
            _optionSetDetailCreaterService.createMany(entity.getItems());
        }
        return success;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean updateMany(List<OptionSet> entity) throws XmsException, SQLException {
        if (CollectionUtil.isEmpty(entity)) return false;
        List<OptionSet> entities = super.listByIds(entity.stream().map(x -> x.getOptionsetid()).collect(Collectors.toList()));
        for (OptionSet item : entities) {
            this.update(item);
        }
        return true;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean update(Wrapper<OptionSet> updateWrapper) {
        return super.update(updateWrapper);
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean update(OptionSet entity, Wrapper<OptionSet> updateWrapper) {
        return super.update(entity, updateWrapper);
    }
}
